package com.rfid.manager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;

import com.rfid.a252robot.App;
import com.rfid.a252robot.config.GlobalConfig;
import com.rfid.common.log.LogUtils;
import com.spectre.generate.ConfigManager;

public class UsbPermissionReceiver extends BroadcastReceiver {

    public static String ACTION_DEVICE_PERMISSION = "com.rfid.manager.ACTION_UsbPermissionReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_DEVICE_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if(device != null){
                        int vendorId = device.getVendorId();
                        int productId = device.getProductId();
                        LogUtils.v("getPermission > " + device.getProductName() + ":" + productId + ":" + vendorId);
                        GlobalConfig config = ConfigManager.INSTANCE.getGlobalConfig();
                        if (Integer.valueOf(config.getDoorPid(),16) == productId && Integer.valueOf(config.getDoorVid(),16) == vendorId) {
                            DoorBoxManager.INSTANCE.init(App.getApp());
                            DoorBoxManager.INSTANCE.prepareReadWrite();
                        } else if (Integer.valueOf(config.getTagReaderPid(),16) == productId && Integer.valueOf(config.getTagReaderVid(),16) == vendorId) {
                            ReadTagManager.INSTANCE.findDevice();
                            ReadTagManager.INSTANCE.prepareReadTag();
                        }
                    }
                }
                else {
                    LogUtils.d( "permission denied for device " + device);
                }
            }
        } else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            LogUtils.v("ACTION_USB_DEVICE_ATTACHED " + device);
            int vendorId = device.getVendorId();
            int productId = device.getProductId();
            GlobalConfig config = ConfigManager.INSTANCE.getGlobalConfig();
            if (Integer.valueOf(config.getDoorPid(),16) == productId && Integer.valueOf(config.getDoorVid(),16) == vendorId) {
                if (usbManager.hasPermission(device)) {
                    DoorBoxManager.INSTANCE.init(App.getApp());
                    DoorBoxManager.INSTANCE.prepareReadWrite();
                } else {
                    DoorBoxManager.INSTANCE.requestPermission(device);
                }
            } else if (Integer.valueOf(config.getTagReaderPid(),16) == productId && Integer.valueOf(config.getTagReaderVid(),16) == vendorId) {
                if (usbManager.hasPermission(device)) {
                    ReadTagManager.INSTANCE.findDevice();
                    ReadTagManager.INSTANCE.prepareReadTag();
                } else {
                    ReadTagManager.INSTANCE.requestPermission(device);
                }
            }
        }
    }
}
